package org.wabson.alfresco.translations;

public class BundleCompare
{
   public static void main(String[] args)
   {
      if (args.length < 2)
      {
         System.out.println("Usage: BundleCompare <action> <baseDir> <compareDir>");
         System.exit(1);
      }

      if (!args[0].equals("compare") && !args[0].equals("compareversions") && !args[0].equals("format"))
      {
          System.out.println("Unrecognised action " + args[0]);
          System.exit(1);
      }
      
      if (args[0].equals("compare")) // Compare translation
      {
          String release = args[2]; // e.g. 'community-3.2r', 'enterprise-3.2.0r'
          String[] rcmpts = release.split("-");
          String jar = "jar:/alfresco/bundles/alfresco-" + rcmpts[0] + "-messages-" + rcmpts[1] + ".jar";

          // Repository messages
          compareDirectories(jar + "!alfresco/messages", 
                  args[1] + "/messages", 
                  null, 
                  "slingshot.properties;webscripts.properties;webclient.properties;mobile.properties;dod5015.properties".split(";"), 
                  false);

          // Web client
          compareDirectories(jar + "!alfresco/messages", 
                  args[1] + "/messages", 
                  "webclient.properties".split(";"), 
                  null, 
                  false);

          // Slingshot global messages
          compareDirectories(jar + "!alfresco/messages", 
                  args[1] + "/messages", 
                  "slingshot.properties".split(";"), 
                  null, 
                  false);

          // Slingshot web script messages
          compareDirectories(jar + "!alfresco/site-webscripts", 
                  args[1] + "/site-webscripts", 
                  null, 
                  "org/alfresco/components/footer/footer.get.properties".split(";"), 
                  false);
      }
      
      if (args[0].equals("compareversions")) // Compare versions of Alfresco
      {
          String[] files = null;
          String[] ignoreFiles = null;
          if (args.length > 3)
          {
              files = args[3].split(";");
          }
          if (args.length > 3)
          {
              ignoreFiles = args[3].split(";");
          }
          compareDirectories(args[1], args[2], files, ignoreFiles, true);
      }
      
      if (args[0].equals("format")) // Compare versions of Alfresco
      {
          // TODO Add support for formatting translated files
      }
   }
   
   private static void compareDirectories(String d1, String d2, String[] bundles, String[] ignoreBundles, boolean compareValues)
   {
       String bundleFiles = "", ignoreBundleFiles = "";
       if (bundles != null)
       {
           for (String b : bundles)
           {
               if (!bundleFiles.equals(""))
                   bundleFiles = bundleFiles + ", ";
               bundleFiles = bundleFiles + b;
           }
       }
       if (ignoreBundles != null)
       {
           for (String b : ignoreBundles)
           {
               if (!ignoreBundleFiles.equals(""))
                   ignoreBundleFiles = ignoreBundleFiles + ", ";
               ignoreBundleFiles = ignoreBundleFiles + b;
           }
       }
       
         System.out.println("**************************\r\nComparing directories\r\n"
                 + d1 + "\r\n" + d2 + "\r\n"
                 + "Bundle files: " + bundleFiles + "\r\n" + "Ignore files: " + ignoreBundleFiles + "\r\n"
                 + "\r\n**************************\r\n");

         BundleLoader l1, l2;
         
         if (d1.startsWith("classpath:"))
         {
             l1 = new ClassPathLoader(d1.substring(10));
         }
         else if (d1.startsWith("jar:"))
         {
             String jar = d1.substring(4);
             if (jar.indexOf("!") != -1)
             {
                 l1 = new ClassPathJarLoader(jar.substring(0, jar.indexOf("!")), jar.substring(jar.indexOf("!") + 1));
             }
             else
             {
                 l1 = new ClassPathJarLoader(jar, "");
             }
         }
         else
         {
             l1 = new FileSystemLoader(d1);
         }
         
         if (d2.startsWith("classpath:"))
         {
             l2 = new ClassPathLoader(d2.substring(10));
         }
         else if (d2.startsWith("jar:"))
         {
             String jar = d2.substring(4);
             if (jar.indexOf("!") != -1)
             {
                 l2 = new ClassPathJarLoader(jar.substring(0, jar.indexOf("!")), jar.substring(jar.indexOf("!") + 1));
             }
             else
             {
                 l2 = new ClassPathJarLoader(jar, "");
             }
         }
         else
         {
             l2 = new FileSystemLoader(d2);
         }
         
         BundleGroupComparator gc = new BundleGroupComparator(l2, l1);
         BundleGroupComparisonResult gcr = gc.compare(bundles, ignoreBundles, compareValues);
        
        for (String bundle : gcr.getBundles())
        {
            int state = gcr.getBundleState(bundle);
            
            if (state == BundleGroupComparisonResult.BUNDLE_STATE_EXTRA)
            {
                System.out.println(bundle);
                System.out.println("Only in " + d2 + "\r\n");
            }
            else if (state == BundleGroupComparisonResult.BUNDLE_STATE_MISSING)
            {
                System.out.println(bundle);
                System.out.println("Only in " + d1 + "\r\n");
            }
            else // OK
            {
                BundleComparisonResult bundleResult = gcr.getComparisonResult(bundle);
                
                if (!bundleResult.isEmpty())
                {
                    StringBuffer buf = new StringBuffer();
                    for (MessageComparisonResult propResult : bundleResult.getPropertyComparisons())
                    {
                        if (propResult.getThisValue() == null)
                        {
                            buf.append("- " + propResult.getPropertyName() + "=" + propResult.getCompareToValue() + "\r\n");
                        }
                        else if (propResult.getCompareToValue() == null)
                        {
                            buf.append("+ " + propResult.getPropertyName() + "=" + propResult.getThisValue() + "\r\n");
                        }
                        else
                        {
                            buf.append("< " + propResult.getPropertyName() + "=" + propResult.getCompareToValue() + "\r\n");
                            buf.append("> " + propResult.getPropertyName() + "=" + propResult.getThisValue() + "\r\n");
                        }
                    }
                    System.out.println(bundle);
                    System.out.println(buf.toString());
                }
            }
        }

        System.out.println("**************************\r\nSUMMARY\r\n**************************\r\n");
        
        // Print summary
        int totalFiles = gcr.getNumBundles(), totalMessages = gcr.getNumMessages();
        int missingMessages = gcr.getNumMissingMessages(), extraMessages = gcr.getNumExtraMessages();
        int missingBundles = gcr.getNumMissingBundles(), extraBundles = gcr.getNumExtraBundles();
        int fileCoverage = (int) (gcr.getBundleCoverage() * 100F);
        int messageCoverage = (int) (gcr.getMessageCoverage() * 100F);
        System.out.println("Analysed " + totalMessages + " source messages in " + totalFiles + " files");
        System.out.println("Identified " + missingMessages + " missing messages and " + missingBundles + " missing files");
        System.out.println("Ignored " + extraMessages + " extra messages and " + extraBundles + " extra files");
        System.out.println("File coverage: " + fileCoverage + "%");
        System.out.println("Message coverage: " + messageCoverage + "%");
        
        System.out.println("\r\n**************************");
   }

}
